﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace FileNameChangeApplication3._0
{
    public partial class Form1 : Form
    {
        private List<string> imgs = new List<string>();

        private int imgNow = -1;

        private string oldZdh = "";

        private string newZdh = "";

        private string dirFullPath = "";

        private string imgName = "";

        private bool openImgSuccess = false;

        private bool error = false;

        string errorImgTxtFulPath = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void selectDirButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbGDB = new FolderBrowserDialog();
            fbGDB.Description = "请选择图片";
            if (fbGDB.ShowDialog() == DialogResult.OK)
            {
                imgs.Clear();
                imgNow = 0;
                oldZdh = "";
                newZdh = "";
                dirFullPath = "";
                dirPathTextBox.Text = fbGDB.SelectedPath;
                GetAllImg(dirPathTextBox.Text.Trim());
                errorImgTxtFulPath = Directory.GetCurrentDirectory() + "\\errorImg\\" + DateTime.Now.ToLocalTime().ToString("yyyyMMdd_HHmmss") + ".txt";

                showImage();
                
            }
        }

        private void showImage()
        {
            openImgSuccess = false;
            if (imgs.Count <= 0)
            {
                MessageBox.Show("当前文件夹内没有照片！！");
            }
            else
            {
                int flag = 0;
                if (imgNow < 0)
                {
                    imgNow = 0;
                    flag = 1;
                }
                if (imgNow >= imgs.Count)
                {
                    flag = 2;
                    imgNow = imgs.Count - 1;
                }
                try
                {
                    string imgFullPath = imgs[imgNow];
                    if (!File.Exists(imgFullPath))
                    {
                        imgs.RemoveAt(imgNow);
                        imgFullPath = imgs[imgNow];
                        if (!File.Exists(imgFullPath))
                        {
                            imgNow--;
                            imgFullPath = imgs[imgNow];
                        }
                    }
                    FileStream pFileStream = new FileStream(imgs[imgNow], FileMode.Open, FileAccess.Read);
                    pictureBox1.Image = Image.FromStream(pFileStream);
                    pFileStream.Close();
                    pFileStream.Dispose();
                    openImgSuccess = true;
                    imgsCountLable.Text = imgs.Count.ToString();
                    imgNowTextBox.Text = (imgNow + 1).ToString();
                    string[] imgPaths = imgFullPath.Split('\\');
                    imgName = imgPaths[imgPaths.Length - 1];
                    oldZdh = newZdh;
                    newZdh = imgPaths[imgPaths.Length - 2];
                    dirFullPath = Path.GetDirectoryName(imgFullPath);
                    imgName = Path.GetFileName(imgFullPath);
                    if (File.Exists(dirFullPath + "\\error"))
                    {
                        errorImgcheckBox.Checked = true;
                    }
                    else
                    {
                        errorImgcheckBox.Checked = false;
                    }
                    if (!oldZdh.Equals(newZdh))
                    {
                       
                    }
                    zdhLabel.Text = newZdh;
                    dhhmTextBox.Text = "";
                   
                    jfndTextBox.Text = "";
                    yhzgxTextBox.Text = "";
                    yyqlrgxTextBox.Text = "";
                    bz1TextBox.Text = "";
                    xzzdCheckBox.Checked = false;
                    int plusCount = imgName.Length - imgName.Replace("+", "").Length;
                    if (plusCount == 1)
                    {
                        string[] strs = imgName.Split('+');
                        foreach(char ch in strs[0])
                        {
                            dhhmTextBox.Text += ch.ToString();
                        }
                        jfndTextBox.Text = strs[1].Substring(0, strs[1].Length - 4);
                    }
                    if (plusCount == 5)
                    {
                        string[] strs = imgName.Split('+');
                        if (!strs[0].Equals("@"))
                        {
                            foreach (char ch in strs[0])
                            {
                                dhhmTextBox.Text += ch.ToString();
                            }
                        }
                        if (!strs[1].Equals("@"))
                        {
                            jfndTextBox.Text = strs[1];
                        }
                        if (!strs[2].Equals("@"))
                        {
                            yhzgxTextBox.Text = strs[2];
                        }
                        if (!strs[3].Equals("@"))
                        {
                            yyqlrgxTextBox.Text = strs[3];
                        }
                        if (!strs[4].Equals("@"))
                        {
                            bz1TextBox.Text = strs[4];
                        }
                        if (strs[5][0].Equals('1'))
                        {
                            xzzdCheckBox.Checked = true;
                        }

                    }

                    imgNameLable.Text = imgName;
                    dirPathTextBox.Focus();
                    

                    if (flag == 1)
                    {
                        MessageBox.Show("当前显示的是第一张照片");
                    }
                    if (flag == 2)
                    {
                        MessageBox.Show("当前显示的是最后一张照片");
                    }

                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message.ToString());
                }
               
            }
        }

        public void GetAllImg(string strBaseDir)
        {
            DirectoryInfo di = new DirectoryInfo(strBaseDir);
            DirectoryInfo[] arrDi = di.GetDirectories();
            foreach (DirectoryInfo d in arrDi)
            {
                StartWork(d.FullName);
                GetAllImg(d.FullName);
            }
        }
        private void StartWork(string filePath)
        {
            string[] files = Directory.GetFiles(filePath);

            if (files.Length != 0)
            {
                foreach (string str in files)
                {
                    if (str.Substring(str.Length - 4, 4).ToLower() == ".png" || str.Substring(str.Length - 4, 4).ToLower() == ".jpg")
                    {
                        imgs.Add(str);
                    }
                }
            }
        }

        private void openFileDirButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                System.Diagnostics.Process.Start("explorer.exe", dirFullPath);
            }
        }

        private void imgNowTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (openImgSuccess)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    imgNow = Convert.ToInt32(imgNowTextBox.Text) - 1;
                    showImage();
                    
                }
            }
            
        }

        private void imgNowTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            if ((e.KeyChar >= '0' && e.KeyChar <= '9')||e.KeyChar==8)
            {
                e.Handled = false;
            }
        }

        private void preButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                imgNow--;
                showImage();
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                imgNow++;
                showImage();
            }
        }

        private void leftRotButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                pictureBox1.Image = pictureBox1.Image.GetRotateImage(90);
            }
        }

        private void rightRotButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                pictureBox1.Image = pictureBox1.Image.GetRotateImage(-90);
            }
        }

   

       

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(dhhmTextBox.Focused || jfndTextBox.Focused || yhzgxTextBox.Focused || yyqlrgxTextBox.Focused || bz1TextBox.Focused  || xzzdCheckBox.Focused)) 
            {
                if (e.KeyCode == Keys.A)
                {
                    preButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.D)
                {
                    nextButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.Q)
                {
                    leftRotButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.E)
                {
                    rightRotButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.W)
                {
                    sfzFrontButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.S)
                {
                    hkbButton_Click(sender, e);
                }
                if (e.KeyCode == Keys.X)
                {
                    sfzBackButton_Click(sender, e);
                }
            }
            if (e.KeyCode == Keys.Escape)
            {
                dirPathTextBox.Focus();
            }
        }

        private void sfzFrontButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "身份证01";
                string strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                if (File.Exists(strNewFullName))
                {
                    if (strOldFullName.Equals(strNewFullName))
                    {
                        nextButton_Click(sender, e);
                        return;
                    }
                    else
                    {
                        for (int i = 1; i <= 99; i++)
                        {
                            //if (i < 10)
                            //{
                            //    strNewReplaceName = "身份证01_0" + i.ToString();
                            //}
                            //else
                            //{
                            //    strNewReplaceName = "身份证01_" + i.ToString();
                            //}
                            strNewReplaceName = "身份证01_" + i.ToString("00");
                            strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                            if (!File.Exists(strNewFullName))
                            {
                                break;
                            }
                            else
                            {
                                if (strOldFullName.Equals(strNewFullName))
                                {
                                    nextButton_Click(sender, e);
                                    return;
                                }
                            }
                        }
                    }

                }
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch
                {
                    MessageBox.Show("未知错误，请在文件夹内手动修改");
                    openFileDirButton_Click(sender, e);
                }
            }
            

        }

        private void hkbButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "";
                string strNewFullName = "";

                for (int i = 1; i <= 99; i++)
                {
                    //if (i < 10)
                    //{
                    //    strNewReplaceName = "户口簿0" + i.ToString();
                    //}
                    //else
                    //{
                    //    strNewReplaceName = "户口簿" + i.ToString();
                    //}
                    strNewReplaceName = "户口簿" + i.ToString("00");
                    strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                    if (!File.Exists(strNewFullName))
                    {
                        break;
                    }
                    else
                    {
                        if (strOldFullName.Equals(strNewFullName))
                        {
                            nextButton_Click(sender, e);
                            return;
                        }
                    }
                }
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch
                {
                    MessageBox.Show("未知错误，请在文件夹内手动修改");
                    openFileDirButton_Click(sender, e);
                }
            }

        }

        private void sfzBackButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "身份证";
                string strNewFullName = "";

                for (int i = 2; i <= 99; i++)
                {
                    //if (i < 10)
                    //{
                    //    strNewReplaceName = "身份证0" + i.ToString();
                    //}
                    //else
                    //{
                    //    strNewReplaceName = "身份证" + i.ToString();
                    //}
                    strNewReplaceName = "身份证" + i.ToString("00");
                    strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                    if (!File.Exists(strNewFullName))
                    {
                        break;
                    }
                    else
                    {
                        if (strOldFullName.Equals(strNewFullName))
                        {
                            nextButton_Click(sender, e);
                            return;
                        }
                    }
                }
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch
                {
                    MessageBox.Show("未知错误，请在文件夹内手动修改");
                    openFileDirButton_Click(sender, e);
                }
            }
        }

        private void dhhmTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender,e);
            }
        }

        private void jfndTextBox_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender, e);
            }
        }

        private void yhzgxTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender, e);
            }
        }

        private void yyqlrgxTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender, e);
            }
        }
        


        private void bz1TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender, e);
            }
        }
      
        private void xzzdCheckBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                messageButton_Click(sender, e);
            }
        }
        

        private void messageButton_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "";
                string strNewFullName = "";


                if (dhhmTextBox.Text.Trim().Equals(""))
                {
                    strNewReplaceName += "@";
                }
                else
                {
                    strNewReplaceName += dhhmTextBox.Text.Trim().Replace("-", "");
                }
                if (jfndTextBox.Text.Trim().Equals(""))
                {
                    strNewReplaceName += "+@";
                }
                else
                {
                    strNewReplaceName += "+" + jfndTextBox.Text.Trim();
                }
                if (yhzgxTextBox.Text.Trim().Equals(""))
                {
                    strNewReplaceName += "+@";
                }
                else
                {
                    strNewReplaceName += "+" + yhzgxTextBox.Text.Trim();
                }
                if (yyqlrgxTextBox.Text.Trim().Equals(""))
                {
                    strNewReplaceName += "+@";
                }
                else
                {
                    strNewReplaceName += "+" + yyqlrgxTextBox.Text.Trim();
                }
                if (bz1TextBox.Text.Trim().Equals(""))
                {
                    strNewReplaceName += "+@";
                }
                else
                {
                    strNewReplaceName += "+" + bz1TextBox.Text.Trim();
                }
                if (xzzdCheckBox.Checked)
                {
                    strNewReplaceName += "+1";
                }
                else
                {
                    strNewReplaceName += "+0";
                }
                strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                if (File.Exists(strNewFullName))
                {
                    for (int i = 1; i <= 99; i++)
                    {
                        strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName + "_" + i.ToString("00"));


                        if (!File.Exists(strNewFullName))
                        {
                            break;
                        }
                        else
                        {
                            if (strOldFullName.Equals(strNewFullName))
                            {
                                nextButton_Click(sender, e);
                                return;
                            }
                        }
                    }
                }
                
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    openFileDirButton_Click(sender, e);
                }
            }
            
        }

        private void dhhmTextBox_TextChanged(object sender, EventArgs e)
        {

            if (dhhmTextBox.Text.Length == 3)
            {
                dhhmTextBox.Text += "-";
                dhhmTextBox.Select(4, 0);
                return;
            }
            if (dhhmTextBox.Text.Length == 8)
            {
                dhhmTextBox.Text += "-";
                dhhmTextBox.Select(9, 0);
                return;
            }
        }

        private void dhhmTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            if ((dhhmTextBox.Text.Length == 4 || dhhmTextBox.Text.Length == 9) && e.KeyChar == 8) 
            {
                dhhmTextBox.Text = dhhmTextBox.Text.Substring(0, dhhmTextBox.Text.Length - 2);
                dhhmTextBox.Select(dhhmTextBox.Text.Length, 0);
                e.Handled = true;
            }
        }

        private void jfndTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 8) 
            {
                e.Handled = false;
            }
            
        }

        private void errorImgcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                if (errorImgcheckBox.Checked)
                {
                    File.Create(dirFullPath + "\\error");
                }
                else
                {
                    if(File.Exists(dirFullPath + "\\error"))
                    {
                        File.Delete(dirFullPath + "\\error");
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "";
                string strNewFullName = "";

                for (int i = 1; i <= 99; i++)
                {
                    strNewReplaceName = "买卖协议" + i.ToString("00");
                    strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                    if (!File.Exists(strNewFullName))
                    {
                        break;
                    }
                    else
                    {
                        if (strOldFullName.Equals(strNewFullName))
                        {
                            nextButton_Click(sender, e);
                            return;
                        }
                    }
                }
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch
                {
                    MessageBox.Show("未知错误，请在文件夹内手动修改");
                    openFileDirButton_Click(sender, e);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openImgSuccess)
            {
                string strOldFullName = imgs[imgNow];
                string strOldReplaceName = imgName.Substring(0, imgName.Length - 4);
                string strNewReplaceName = "";
                string strNewFullName = "";

                for (int i = 1; i <= 99; i++)
                {
                    
                    strNewReplaceName = "其他" + i.ToString("00");
                    strNewFullName = strOldFullName.Replace(strOldReplaceName, strNewReplaceName);
                    if (!File.Exists(strNewFullName))
                    {
                        break;
                    }
                    else
                    {
                        if (strOldFullName.Equals(strNewFullName))
                        {
                            nextButton_Click(sender, e);
                            return;
                        }
                    }
                }
                try
                {
                    File.Move(strOldFullName, strNewFullName);
                    imgs[imgNow] = strNewFullName;
                    nextButton_Click(sender, e);
                }
                catch
                {
                    MessageBox.Show("未知错误，请在文件夹内手动修改");
                    openFileDirButton_Click(sender, e);
                }
            }
        }

        private void zdhLabel_TextChanged(object sender, EventArgs e)
        {
            if (zdhLabel.ForeColor == Color.Red)
            {
                zdhLabel.ForeColor = Color.Blue;
            }
            else
            {
                zdhLabel.ForeColor = Color.Red;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string binPath = Directory.GetCurrentDirectory();
            Process.Start("explorer.exe", binPath + "\\helpPdf\\");
            Process.Start(binPath + "\\helpPdf\\文件名便捷更改程序帮助文档_20200116.pdf");
        }
    }
}
